Completed
Push — master ( 900d15...0f80c8 )
by Sander
12s
created

angular.controller(ꞌSharingSettingsCtrlꞌ)   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 8
dl 0
loc 64
rs 9.3956

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
	/**
26
	 * Created by wolfi on 25/09/16.
27
	 */
28
	angular.module('passmanApp')
29
		.controller('SharingSettingsCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'ShareService', 'EncryptService',
30
			function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, ShareService, EncryptService) {
31
				$scope.active_vault = VaultService.getActiveVault();
32
				$scope.sharing_keys = angular.copy(ShareService.getSharingKeys());
33
34
				$scope.progress = 1;
35
				$scope.generating = false;
36
37
38
				$scope.available_sizes = [
39
					{
40
						size: 1024,
41
						name: 1024
42
					},
43
					{
44
						size: 2048,
45
						name: 2048
46
					},
47
					{
48
						size: 4096,
49
						name: 4096
50
					}
51
				];
52
53
				$scope.setKeySize = function (size) {
54
					for (var i = 0; i < $scope.available_sizes.length; i++) {
55
						if ($scope.available_sizes[i].size === size) {
56
							$scope.key_size = $scope.available_sizes[i];
57
							return;
58
						}
59
					}
60
				};
61
62
				$scope.setKeySize(2048);
63
64
				$scope.generateKeys = function (length) {
65
					$scope.progress = 1;
66
					$scope.generating = true;
67
68
					ShareService.generateRSAKeys(length).progress(function (progress) {
69
						$scope.progress = progress > 0 ? 2 : 1;
70
						$scope.$digest();
71
					}).then(function (kp) {
72
						$scope.generating = false;
73
74
						var pem = ShareService.rsaKeyPairToPEM(kp);
75
76
						$scope.active_vault.private_sharing_key = EncryptService.encryptString(pem.privateKey);
77
						$scope.active_vault.public_sharing_key = pem.publicKey;
78
79
						VaultService.updateSharingKeys($scope.active_vault).then(function () {
80
							$scope.sharing_keys = ShareService.getSharingKeys();
81
						});
82
					});
83
				};
84
85
				$scope.updateSharingKeys = function () {
86
					$scope.active_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
87
					$scope.active_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
88
					VaultService.updateSharingKeys($scope.active_vault).then(function () {
89
						$scope.sharing_keys = ShareService.getSharingKeys();
90
					});
91
				};
92
93
			}]);
94
}());